package net.wzyjerry.springboot.demo.serviceImpl;

import net.wzyjerry.springboot.demo.SpringBootDemoApplication;
import net.wzyjerry.springboot.demo.model.Value;
import net.wzyjerry.springboot.demo.repository.ValueRepository;
import net.wzyjerry.springboot.demo.service.ValueService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

/**
 * Created by WzyJerry on 2017/10/21.
 */
@Service
public class ValueServiceImpl implements ValueService {

    /**
     * Value repository
     */
    private ValueRepository valueRepository;

    private SpringBootDemoApplication.Settings settings;

    /**
     * Construction of valueService
     *
     * @param valueRepository Dependence injection of valueRepository
     * @param settings        DI of settings
     */
    public ValueServiceImpl(ValueRepository valueRepository,
                            SpringBootDemoApplication.Settings settings) {
        this.valueRepository = valueRepository;
        this.settings = settings;
    }

    /**
     * Create value
     *
     * @param value Value
     * @return Created value
     */
    @Override
    public Value Create(Value value) {
        return valueRepository.save(value);
    }

    /**
     * Update Value
     *
     * @param value Value
     */
    @Override
    public void Update(Value value) {
        valueRepository.save(value);
    }

    /**
     * Retrieve value
     *
     * @param id Value id
     * @return Value
     */
    @Override
    public Value Retrieve(Integer id) {
        return valueRepository.findOne(id);
    }

    /**
     * Delete value
     *
     * @param id Value id
     */
    @Override
    public void Delete(Integer id) {
        valueRepository.delete(id);
    }

    /**
     * Get value page
     *
     * @param index page number
     * @return Page of value
     */
    @Override
    public Page<Value> Page(Integer index) {
        Pageable pageable = new PageRequest(index - 1, settings.getPageSize(), Sort.Direction.ASC, "id");
        return valueRepository.findAll(pageable);
    }

    /**
     * Get the position of the entity(id)
     *
     * @param id Id
     * @return Position
     */
    @Override
    public Integer GetPosition(Integer id) {
        return valueRepository.GetPosition(id);
    }
}
